package win.larryzeal.logdemo.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;


@EnableTransactionManagement
@Configuration
public class DataSourceConfig {
	Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
	
	@Autowired
	private win.larryzeal.logdemo.config.props.DruidDataSourceProperties druidDataSourceProperties;

	@Bean
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		// jdbc
		dataSource.setDriverClassName(this.druidDataSourceProperties.getDriverClass());
		dataSource.setUrl(this.druidDataSourceProperties.getUrl());
		dataSource.setUsername(this.druidDataSourceProperties.getUsername());
		dataSource.setPassword(this.druidDataSourceProperties.getPassword());

		// druid specific
		dataSource.setMaxActive(this.druidDataSourceProperties.getMaxActive());
		dataSource.setInitialSize(this.druidDataSourceProperties.getInitialSize());
		dataSource.setMaxWait(this.druidDataSourceProperties.getMaxWait());
		dataSource.setMinIdle(this.druidDataSourceProperties.getMinIdle());
		dataSource.setTimeBetweenEvictionRunsMillis(this.druidDataSourceProperties.getTimeBetweenEvictionRunsMillis());
		dataSource.setMinEvictableIdleTimeMillis(this.druidDataSourceProperties.getMinEvictableIdleTimeMillis());

		//
		dataSource.setValidationQuery(this.druidDataSourceProperties.getValidationQuery());
		dataSource.setTestWhileIdle(this.druidDataSourceProperties.getTestWhileIdle());
		dataSource.setTestOnBorrow(this.druidDataSourceProperties.getTestOnBorrow());
		dataSource.setTestOnReturn(this.druidDataSourceProperties.getTestOnReturn());
		dataSource.setMaxOpenPreparedStatements(this.druidDataSourceProperties.getMaxOpenPreparedStatements());
		//
		dataSource.setRemoveAbandoned(this.druidDataSourceProperties.getRemoveAbandoned());
		dataSource.setRemoveAbandonedTimeout(this.druidDataSourceProperties.getRemoveAbandonedTimeout());
		dataSource.setLogAbandoned(this.druidDataSourceProperties.getLogAbandoned());

		try {
			dataSource.setFilters(this.druidDataSourceProperties.getFilters());
		} catch (SQLException e) {
			logger.error("DataSource设置filters时发生异常！filters：" + this.druidDataSourceProperties.getFilters());
		}

		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		return new JdbcTemplate(dataSource());
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}
}
